package cn.wikiflyer.beikang.act.user.setting;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class UserSettingFeedAct extends BaseActivity {

	@ViewInject(id = R.id.feed_edit)
	private EditText edit;

	private boolean needSave = true;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_setting_feedback);
	}

	@Override
	protected void onStart() {
		super.onStart();
		try {
			String txt = app().sp.getString("feedback_txt", "");
			if (txt != null) {
				edit.setText("" + txt);
				edit.setSelection(edit.getText().toString().length());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		try {
			if (needSave) {
				String txt = edit.getText().toString();
				app().sp.setString("feedback_txt", "" + txt);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void init() {
		// header.rightTxt.setTextColor(getResources().getColor(R.color.feed_txt));
		getHeader().setRightTxt("");
		getHeader().setClickListener(null, null, new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (TextUtils.isEmpty(edit.getText())) {
					// WKApplication.showToast("请填写您的宝贵意见或建议");
				} else {
					new FeedBackTask(ctx);
				}
			}
		});

		// 获取编辑框焦点
		edit.setFocusable(true);
		edit.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String ss = s.toString();
				if (ss == null || ss.equals("")) {
					getHeader().setRightTxt("");
				} else {
					getHeader().setRightTxt("提交");
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			public void afterTextChanged(Editable s) {

			}
		});
	}

	@Override
	public void setData() {

	}

	/**
	 * 反馈意见
	 */
	class FeedBackTask extends WKAsyncTaskPro {
		public FeedBackTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.postFeedBack("" + edit.getText().toString());
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				needSave = false;
				Toast.makeText(ctx, "反馈成功", Toast.LENGTH_SHORT).show();
				UserSettingFeedAct.this.finish();
			} else {
			}
		}
	}

}
